<template>
	<!-- 考试界面 -->
	<view class="examination-content">
		<view class="ability-box">
			<view class="topic-title">
				考试题目
			</view>
			<view class="ability-area">
				<view class="exam-info">
					<view class="info-item">
						<view class="item-title">考场号</view>
						<view>01考台</view>
					</view>
					<view class="info-item">
						<view class="item-title">考生信息</view>
						<view>姓名</view>
						<view>张三</view>
						<view>性别</view>
						<view>男</view>
					</view>
					<view class="info-item">
						<view class="item-title">科目</view>
						<view>科目一</view>
					</view>
					<view class="info-item">
						<view class="item-title">剩余时间</view>
						<view>{{times || '44:59'}}</view>
					</view>
				</view>
				<view class="topic-info">
					<view class="topic">
						{{currentTopic.topicText}}
						<view v-for="item in currentTopic.topicOptions">{{item.name}}、{{item.text}}</view>
					</view>
					<view class="choice-box">
						<view class="choice-title">你选择的答案</view>
						<view class="option-list">
							<view class="option-list1" v-if="currentTopic.key == ''">
								<view class="option-item" :class="item.isSelect ? 'is-select' : ''"
									v-for="(item,index) in currentTopic.topicOptions" :key="index"
									@click="choice(item)">
									{{item.name}}
								</view>
							</view>
							<view class="option-list1" v-if="currentTopic.key != ''">
								<view class="option-item" :class="item.isSelect ? 'is-select' : ''"
									v-for="(item,index) in currentTopic.topicOptions" :key="index">
									{{item.name}}
								</view>
							</view>

						</view>
					</view>
					<u-popup v-model="show" mode="center" @open="openPopup" width="690rpx" height="720rpx"
						border-radius="20">
						<view class="popup-box">
							<view class="topic-popup">
								{{currentTopic.topicText}}
							</view>
							<view class="bm-box">
								<view>正确答案:{{currentTopic.trueKey}} 您的选择:{{currentTopic.key}}</view>
								<view @click="closePopup">确定</view>
								<view>页面将在{{popupTiming}}秒后自动关闭，返回主考试界面</view>
							</view>
						</view>
					</u-popup>
				</view>
				<view class="topic-list">
					<scroll-view scroll-y="true" style="height: 800rpx;">
						<view class="table-head">
							<view>1列</view>
							<view>2列</view>
							<view>3列</view>
							<view>4列</view>
							<view>5列</view>
						</view>
						<view class="table-head">
							<view :class="item.ischecked ? 'is-select' : ''" v-for="(item,index) in tableData"
								:key="index" @click="selectTopic(item,index)">
								<view :class="item.key == item.trueKey ? '': 'wrong-text'" v-if="item.key != ''">
									{{item.key || index+1}}
								</view>
								<view v-if="item.key == ''">
									{{item.key || index+1}}
								</view>
							</view>
						</view>
					</scroll-view>
				</view>
			</view>
			<view class="fun-area">
				<view @click="chengeTopic(1)" :disabled="upper">上一题</view>
				<view @click="chengeTopic(2)" :disabled="downer">下一题</view>
				<view>读题</view>
				<view class="submit" @click="handlePaper">交卷</view>
			</view>
			<view class="analysis-box">
				<view class="analysis-title">
					<view>操作提示：{{currentTopic.topicType == 1 ? "选择" : "判断"}}</view>
					<view>本题为{{currentTopic.topicType == 1 ? "选择" : "判断"}}题，请在备选答案中选择你认为正确的答案。</view>
				</view>
				<view class="analysis-content">
					<image src="../../../static/index/course.png" mode=""></image>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 弹窗倒计时
				popupTiming: null,
				popupTimer: null,
				// 题目集合
				tableData: [{
						key: '',
						topicType: 1,
						topicText: '申请人有下列何种行为的，三年内不得再次申领机动车驾驶证三年内不得再次申领机动车驾驶证？',
						ischecked: true,
						trueKey: 'A',
						topicOptions: [{
								name: 'A',
								text: '申请人在考试过程中有贿赂行为',
								isSelect: false,
							},
							{
								name: 'B',
								text: '申请人在考试过程中有贿赂行为',
								isSelect: false,
							},
							{
								name: 'C',
								text: '申请人在考试过程中有贿赂行为',
								isSelect: false,
							},
							{
								name: 'D',
								text: '申请人在考试过程中有贿赂行为',
								isSelect: false,
							},
						]
					},
					{
						key: '',
						topicText: '题目2',
						topicType: 1,
						ischecked: false,
						trueKey: 'A',
						topicOptions: [{
								name: 'A',
								isSelect: false,
							},
							{
								name: 'B',
								isSelect: false,
							},
							{
								name: 'C',
								isSelect: false,
							},
							{
								name: 'D',
								isSelect: false,
							},
						]
					},
					{
						key: '',
						topicType: 1,
						topicText: '题目3',
						ischecked: false,
						trueKey: 'A',
						topicOptions: [{
								name: 'A',
								isSelect: false,
							},
							{
								name: 'B',
								isSelect: false,
							},
							{
								name: 'C',
								isSelect: false,
							},
							{
								name: 'D',
								isSelect: false,
							},
						]
					},
					{
						id: 1,
						// 用户选择答案
						key: '',
						// 题目类型
						topicType: 1,
						// 题干 图片 题目选项
						topicText: '题目4',
						ischecked: false,
						trueKey: 'A',
						topicOptions: [{
								name: 'A',
								isSelect: false,
							},
							{
								name: 'B',
								isSelect: false,
							},
							{
								name: 'C',
								isSelect: false,
							},
							{
								name: 'D',
								isSelect: false,
							},
						]
					},
					{
						key: '',
						topicType: 1,
						topicText: '题目5',
						ischecked: false,
						trueKey: 'A',
						topicOptions: [{
								name: 'A',
								isSelect: false,
							},
							{
								name: 'B',
								isSelect: false,
							},
							{
								name: 'C',
								isSelect: false,
							},
							{
								name: 'D',
								isSelect: false,
							},
						]
					},
					{
						key: '',
						topicType: 1,
						topicText: '题目6',
						ischecked: false,
						trueKey: 'A',
						topicOptions: [{
								name: 'A',
								isSelect: false,
							},
							{
								name: 'B',
								isSelect: false,
							},
							{
								name: 'C',
								isSelect: false,
							},
							{
								name: 'D',
								isSelect: false,
							},
						]
					},
					{
						key: '',
						topicType: 1,
						topicText: '题目7',
						ischecked: false,
						trueKey: 'A',
						topicOptions: [{
								name: 'A',
								isSelect: false,
							},
							{
								name: 'B',
								isSelect: false,
							},
							{
								name: 'C',
								isSelect: false,
							},
							{
								name: 'D',
								isSelect: false,
							},
						]
					},
					{
						key: '',
						topicType: 1,
						topicText: '题目8',
						ischecked: false,
						trueKey: 'A',
						topicOptions: [{
								name: 'A',
								isSelect: false,
							},
							{
								name: 'B',
								isSelect: false,
							},
							{
								name: 'C',
								isSelect: false,
							},
							{
								name: 'D',
								isSelect: false,
							},
						]
					},
					{
						key: '',
						topicType: 2,
						topicText: '题目9',
						ischecked: false,
						trueKey: '√',
						topicOptions: [{
								name: '√',
								isSelect: false,
							},
							{
								name: '×',
								isSelect: false,
							}
						]
					}
				],
				// 考试时间
				times: null,
				// 考试结束时间
				endTime: null,
				// 当前题的信息
				currentTopic: {},
				currentTopicIndex: 0,

				// 上一题 下一题 按钮控制器
				upper: false,
				downer: false,

				show: false,

				// 交卷计数器
				// 答对数
				trueNum: 0,
				// 答错数
				wrongNum: 0,
				// 没做数
				notDoNum: 0
			}
		},
		created() {
			this.init()
		},
		methods: {
			// 交卷
			handlePaper() {
				// 计算 答对数 答错数 没做数
				this.tableData.forEach(ele => {
					if (ele.key == ele.trueKey) {
						this.trueKey++
					} else {
						this.wrongNum++
					}

					if (ele.key == '') {
						this.notDoNum++
					}
				})
				let that = this
				uni.showModal({
					title:'操作提示',
					content: `你当前答对:${this.trueNum}，答错:${this.wrongNum}，未答:${this.notDoNum}。
					1.点击【确认交卷】，将提交考试成绩，考试结束！
					2.点击【继续考试】，将关闭此窗口，继续考试！`,
					cancelText: '继续考试',
					cancelColor: '#2AC092',
					confirmText: '确认交卷',
					confirmColor: '#666666',
					success: function(res) {
						if (res.confirm) {
							console.log('用户点击确定');
							that.$emit('getSteps', 3)
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			
			// 答错题目后弹窗
			openPopup() {
				this.popupTiming = 5
				this.popupTimer = setInterval(() => {
					this.popupTiming--
					if (this.popupTiming <= 0) {
						this.show = false
						this.chengeTopic(2)
						clearInterval(this.popupTimer)
					}
				}, 1000)
			},
			// 主动关闭弹窗 跳到下一题
			closePopup() {
				this.show = false
				this.chengeTopic(2)
				clearInterval(this.popupTimer)
			},
			// 选择答案
			choice(item) {
				// 渲染选择的答案按钮
				this.tableData[this.currentTopicIndex].key = item.name
				item.isSelect = true
				// 如果答对了就直接跳到下一题
				if (this.currentTopic.trueKey == item.name) {
					console.log('答对了::');
					this.chengeTopic(2)
				} else {
					// 如果答错了 弹出题目的提示窗口
					this.show = true
				}
			},
			// 初始化页面参数
			init() {
				this.getTime()
				this.currentTopic = this.tableData[0]
			},
			// 判断按钮可否可点击
			isCanClick(type) {
				// type 1 上 2 下
				// 上 且 在第一个题时
				if (type == 1 && this.currentTopicIndex == 0) {
					this.upper = true
					uni.showToast({
						title: '已经是第一题了',
						icon: 'none'
					})
				}
				if (type == 1 && this.currentTopicIndex != 0) {
					this.upper = false
					this.downer = false
					this.currentTopicIndex = this.currentTopicIndex - 1
				}

				if (type == 2 && this.currentTopicIndex == 8) {
					this.downer = true
					uni.showModal({
						title: '提示',
						content: '已经是最后一题了,是否交卷?',
						success: function(res) {
							if (res.confirm) {
								console.log('用户点击确定');
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					})
				}
				if (type == 2 && this.currentTopicIndex != 8) {
					this.downer = false
					this.upper = false
					this.currentTopicIndex = this.currentTopicIndex + 1
				}
			},
			// 切换题目
			chengeTopic(type) {
				this.isCanClick(type)
				this.tableData.forEach(ele => {
					ele.ischecked = false
				})
				console.log(this.currentTopicIndex, 'currentTopicIndex====');
				this.currentTopic = this.tableData[this.currentTopicIndex]
				this.tableData[this.currentTopicIndex].ischecked = true
			},
			// 点击选择题目
			selectTopic(item, index) {
				this.currentTopic = item
				this.currentTopicIndex = index
				this.tableData.forEach(ele => {
					ele.ischecked = false
				})

				item.ischecked = true
			},
			countDown(time) {
				var nowTime = +new Date(); //获取当前总毫秒数
				var inputTime = +new Date(time); //用户输入的总毫秒数
				var times = (inputTime - nowTime) / 1000; //剩余的总秒数
				//转换为分
				var m = parseInt(times / 60 % 60);
				m = m < 10 ? "0" + m : m
				//转换成秒
				var s = parseInt(times % 60);
				s = s < 10 ? "0" + s : s
				return m + ":" + s;
			},
			filterTime(time) {
				const date = new Date(time)
				const Y = date.getFullYear()
				const M = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
				const D = date.getDate()
				const H = date.getHours() // 小时
				const m = date.getMinutes() // 分钟
				const S = date.getSeconds() // 秒
				return `${Y}-${M}-${D} ${H}:${m}:${S}`
			},
			getTime() {
				this.endTime = new Date();
				console.log(this.endTime);
				let min = this.endTime.getMinutes();
				this.endTime = this.endTime.setMinutes(min + 45);
				this.endTime = this.filterTime(this.endTime)
				console.log(this.endTime);

				// return
				setInterval(() => {
					// console.log(this.endTime);
					this.times = this.countDown(this.endTime)
					if (this.times < 0) {
						clearInterval(this.times)
					}
				}, 1000)
			}
		}
	}
</script>

<style lang="scss">
	.wrong-text {
		color: #FF6D6B;
	}


	.is-select {
		background-color: #2AC092 !important;
	}

	.ability-box {
		padding: 20rpx 10rpx;
		background-color: #fff;

		.topic-title {
			margin-left: 20%;
			color: #2AC092;
			font-size: 24rpx;
		}

		.ability-area {
			display: flex;
			font-size: 22rpx;
			padding: 0 10rpx;

			.exam-info {
				padding: 10rpx;
				width: 20%;
				border: 1rpx solid #E6E6E6;

				.info-item {
					text-align: center;
					// border-top: 1rpx solid #E6E6E6;
					border-bottom: 1rpx solid #E6E6E6;

					>view {
						padding: 20rpx 0;
					}

					.item-title {
						color: #2AC092;
					}
				}
			}

			.topic-info {
				padding: 20rpx;
				width: 35%;
				font-size: 30rpx;
				line-height: 1.5;
				border: 1rpx solid #E6E6E6;
				border-left: none;
				position: relative;


				.topic {
					padding-bottom: 250rpx;
				}

				.choice-box {
					position: absolute;
					bottom: 20rpx;
					width: 100%;

					.choice-title {
						color: #2AC092;
					}
				}

				.option-list {
					width: 85%;
					display: flex;
					flex-wrap: wrap;
					justify-content: space-between;

					.option-list1 {
						width: 100%;
						display: flex;
						flex-wrap: wrap;
						justify-content: space-between;
					}

					.option-item {
						font-size: 36rpx;
						margin-top: 20rpx;
						background-color: #F5F5F5;
						width: max-content;
						padding: 10rpx 30rpx;
						border-radius: 6rpx;
						// margin-right: 20rpx;
					}
				}
			}

			.topic-list {
				width: 45%;
				font-size: 24rpx;
				border: 1rpx solid #E6E6E6;
				border-left: none;
				border-top: none;

				.table-head {
					display: flex;
					flex-wrap: wrap;

					>view {
						width: 20%;
						padding: 10rpx;
						border: 1rpx solid #E6E6E6;
						text-align: center;
					}
				}

				// .table-head1 {
				// 	display: flex;
				// 	flex-wrap: wrap;

				// 	>view {
				// 		width: 65rpx;
				// 		padding: 10rpx;
				// 		border: 1rpx solid #E6E6E6;
				// 		text-align: center;
				// 	}
				// }
			}
		}
	}

	.fun-area {
		width: 95%;
		margin: 50rpx auto;
		display: flex;
		justify-content: space-between;
		padding: 20rpx;
		border-radius: 48rpx;
		box-shadow: 0rpx 3rpx 20rpx 0rpx rgba(51, 51, 51, 0.1);

		>view {
			padding: 10rpx 40rpx;
			border-radius: 30rpx;
			color: #2AC092;
			border: 1rpx solid #2AC092;
		}

		.submit {
			color: #fff;
			background: linear-gradient(180deg, #66CCAD, #2AC092);
		}
	}

	.analysis-box {
		padding: 20rpx;
		border: 1rpx solid #E6E6E6;
		border-radius: 10rpx;
		margin-bottom: 50rpx;

		.analysis-title {
			border-bottom: 1rpx solid #E6E6E6;

			>view {
				margin-bottom: 20rpx;
			}
		}

		.analysis-content {
			padding: 20rpx;
		}
	}

	.popup-box {
		padding: 30rpx;

		.topic-popup {
			height: 495rpx;
			border-bottom: 1px solid #E6E6E6;
		}

		.bm-box {
			display: flex;
			justify-content: space-around;
			flex-direction: column;
			align-items: center;
			padding: 20rpx;

			>view:nth-child(2) {
				margin: 10rpx;
				padding: 15rpx 75rpx;
				color: #fff;
				background-color: #2AC092;
				border-radius: 30rpx;
			}

		}
	}
</style>